package com.lanou3g.musicenjoy.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.adapter.MenuActivityAdapter;
import com.lanou3g.musicenjoy.adapter.MenuAdapter;
import com.lanou3g.musicenjoy.adapter.SpecialActivityAdapter;
import com.lanou3g.musicenjoy.entity.Menu;
import com.lanou3g.musicenjoy.entity.MenuOne;
import com.lanou3g.musicenjoy.entity.SpecialOne;
import com.lanou3g.musicenjoy.volley.VolleySingleton;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.ButterKnife;

/**
 * Created by dllo on 15/10/21.
 */
public class MenuActivityFragment extends Fragment implements View.OnClickListener {

    private List<MenuOne.ContentEntity> datas;
    private ListView specialActivityListView;
    private MenuActivityAdapter adapter;
    private RequestQueue queue;
    private ImageView specialActivityImageView;
    private TextView specialname;
    private ImageView specialBack;
    private TextView num;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.activity_special,null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        queue = VolleySingleton.getInstance(getActivity()).getRequestQueue();
        num= (TextView) getActivity().findViewById(R.id.num);
        specialActivityImageView= (ImageView) getActivity().findViewById(R.id.specialActivityImageView);
        specialname= (TextView) getActivity().findViewById(R.id.specialname);
        specialBack= (ImageView) getActivity().findViewById(R.id.specialBack);
        specialBack.setOnClickListener(this);
        specialActivityListView = (ListView) getActivity().findViewById(R.id.specialActivityListView);
        adapter = new MenuActivityAdapter(getActivity());

        specialActivityListView.setAdapter(adapter);
        Bundle bundle = getArguments();
        String a = bundle.getString("b");
        String time=bundle.getString("time");
        String urlH=bundle.getString("url");
        Log.i("dfjaljf", a);
        Picasso.with(getActivity()).load(urlH).into(specialActivityImageView);

        specialname.setText(time);
        String url = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&" +
                "method=baidu.ting.diy.gedanInfo&format=json&listid="+a ;
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response != null) {
                    Gson gson = new Gson();
                    MenuOne result = gson.fromJson(response, MenuOne.class);
                    if (result != null) {
                        datas = result.getContent();

                        adapter.initData(datas);

                    }
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(request);

    }
    //    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_special);
//        ButterKnife.bind(this);
//        queue = VolleySingleton.getInstance(this).getRequestQueue();
//        num= (TextView) findViewById(R.id.num);
//        specialActivityImageView= (ImageView) findViewById(R.id.specialActivityImageView);
//        specialname= (TextView) findViewById(R.id.specialname);
//        specialBack= (ImageView) findViewById(R.id.specialBack);
//        specialBack.setOnClickListener(this);
//        specialActivityListView = (ListView) findViewById(R.id.specialActivityListView);
//        adapter = new MenuActivityAdapter(this);
//
//        specialActivityListView.setAdapter(adapter);
//        Intent intent = getIntent();
//        String a = intent.getStringExtra("b");
//        String time=intent.getStringExtra("time");
//        String urlH=intent.getStringExtra("url");
//        Log.i("dfjaljf", a);
//        Picasso.with(this).load(urlH).into(specialActivityImageView);
//
//        specialname.setText(time);
//        String url = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&" +
//                "method=baidu.ting.diy.gedanInfo&format=json&listid="+a ;
//        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
//            @Override
//            public void onResponse(String response) {
//                if (response != null) {
//                    Gson gson = new Gson();
//                    MenuOne result = gson.fromJson(response, MenuOne.class);
//                    if (result != null) {
//                        datas = result.getContent();
//
//                        adapter.initData(datas);
//
//                    }
//                }
//
//            }
//        }, new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError error) {
//
//            }
//        });
//        queue.add(request);
//    }

    @Override
    public void onClick(View v) {
        getFragmentManager().popBackStack();
    }
}
